const Sequelize = require("sequelize");
const {mysql,isCreateTable} = require("../../config");
//连接数据库
const sequelize = new Sequelize(
  mysql.database,
  mysql.username,
  mysql.password,
  {
    host: mysql.host,
    dialect: "mysql",
    port: mysql.port,
    pool: {
      max: 5,
      min: 0,
    },
    timezone: "+08:00", //时区
    // timezone: mysql.timezone,
    // dialectOptions: {},
    // logging: true, // 只在开发环境下输出MySql日志
    define: {
      paranoid: true,
      createdAt: "created_at",
      updatedAt: "updated_at",
      deletedAt: "deleted_at",
      // 把驼峰命名转换为下划线
      underscored: false,
    },
  }
);
// 是否连接成功
sequelize
  .authenticate()
  .then(() => {
    console.log("数据库:连接成功1");
  })
  .catch((err) => {
    console.log("连接失败", err);
  });

// 根据 model自动创建表
if (isCreateTable) {
  sequelize
    .sync()
    .then(() => {
      console.log("init db ok");
      process.exit(); // 成功之后退出，否则会一直在后台运行
    })
    .catch((err) => {
      console.log("init db error", err);
    });
}

module.exports = { sequelize,Sequelize:Sequelize };
